﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using GenericEntities;
using System.Collections;
using System.Threading;

namespace DiaryPlanner
{
    public partial class Planner : UserControl
    {
        public PlannerMonth plannerMonth = new PlannerMonth();
        public PlannerWeek plannerWeek = new PlannerWeek();
        public PlannerDay plannerDay = new PlannerDay();

        ChangeCalendar changeCalendar;

        public IList plannerilist = new List<GenericEvent>();

        public GenericEvent genericEvent;

        public Planner()
        {
            InitializeComponent();
            initPlanner();
        }

        public void skinSet()
        {
            string skin = UserProfile.Instance().setting;
            if (skin == "b")
            {
                this.BackColor = System.Drawing.Color.PaleTurquoise;

            }
            else if (skin == "c")
            {
                this.BackColor = System.Drawing.Color.LawnGreen;
            }
            else if (skin == "d")
            {
                this.BackColor = System.Drawing.Color.MistyRose;

            }
            else
            { //skin=="a"
                this.BackColor = System.Drawing.SystemColors.ControlLight;

            }
        }

        private void initPlanner()
        {
            CheckForIllegalCrossThreadCalls = false; //스레드 충돌에러 없애기

            plannerMonth.initPlanner(this);
            plannerWeek.initPlanner(this);
            plannerDay.initPlanner(this);

            CalendarView.initPlanner(this, plannerDay, plannerWeek, plannerMonth);

            monthButton.Checked = true;

            changeCalendar = new ChangeMonth(this);

            plannerList.ContextMenu = new ContextMenu();
            plannerList.ContextMenu.MenuItems.Add("삭제", delList);

            CalendarDB.Instance().initLogin();
        }

        private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
        {
            changeCalendar.setDateLabel();
        }

        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            changeCalendar.setDateLabel();
        }

        private void dayButton_CheckedChanged(object sender, EventArgs e)
        {
            changeCalendar = new ChangeMonth(this);
            this.calendarpanel.Controls.Clear();
            this.calendarpanel.Controls.Add(plannerDay);

            plannerDay.viewAllDB();
            plannerDay.setUnSelected();
        }

        private void weekButton_CheckedChanged(object sender, EventArgs e)
        {
            changeCalendar = new ChangeWeek(this, plannerWeek);
            this.calendarpanel.Controls.Clear();
            this.calendarpanel.Controls.Add(plannerWeek);

            plannerWeek.viewAllDB();
            plannerWeek.setUnSelected();
        }

        private void monthButton_CheckedChanged(object sender, EventArgs e)
        {
            changeCalendar = new ChangeMonth(this);
            this.calendarpanel.Controls.Clear();
            this.calendarpanel.Controls.Add(plannerMonth);

            plannerMonth.viewAll();
            //plannerMonth.setUnSelected();
        }

        private void monthButton_CheckedChanged()
        {
            monthButton_CheckedChanged(null,null);
        }

        private void delList(object sender, EventArgs e)
        {
            if (plannerList.SelectedItem == null)
            {
                MessageBox.Show("일정이 선택되지 않았습니다.");
                return;
            }
            
            CalendarDB.Instance().del_db((GenericEvent)plannerilist[plannerList.Items.IndexOf(plannerList.SelectedItem)]);
            CalendarConnection.Instance().delCalendar((GenericEvent)plannerilist[plannerList.Items.IndexOf(plannerList.SelectedItem)]);
            plannerilist.Remove(plannerList.Items.IndexOf(plannerList.SelectedItem));
            plannerList.Items.Remove(plannerList.SelectedItem);
            
            if (monthButton.Checked)
            {
                plannerMonth.viewAll();
            }
            else if (weekButton.Checked)
            {
                plannerWeek.viewAllDB();
            }
            else if (dayButton.Checked)
            {
                plannerDay.viewAllDB();
            }
        }

        private void plannerList_Click(object sender, EventArgs e)
        {
            if (plannerList.SelectedItem == null) return;
        }

        private void plannerList_DoubleClick(object sender, EventArgs e)
        {
            if (plannerList.SelectedItem == null) return;

            genericEvent = (GenericEvent)plannerilist[plannerList.Items.IndexOf(plannerList.SelectedItem)];

            PlannerEdit PlannerEditObject = new PlannerEdit();
            PlannerEditObject.initPlannerEdit(this);
            PlannerEditObject.ShowDialog();
            genericEvent = null;
            plannerDay.viewAllDB();
            plannerWeek.viewAllDB();
            plannerMonth.viewAll();
        }

        public void plannerListClear()
        {
            plannerList.Items.Clear();
            plannerilist.Clear();
        }

        public void plannerListAdd(GenericEvent ge)
        {
            plannerList.Items.Add(ge.Title + " / "+ge.StartTime);
            plannerilist.Add(ge);
        }

        public GenericEvent getSelectList()
        {
            return (GenericEvent)plannerilist[plannerList.Items.IndexOf(plannerList.SelectedItem)];
        }

        public void setDateLabel(DateTime temp)
        {
            temp = temp.AddDays(-6);

            dateLabel.Text = temp.Year.ToString() + "년 " +
                             temp.Month.ToString() + "월 " +
                             temp.Day.ToString() + "일";

            temp = temp.AddDays(6);

            if (temp.AddDays(-6).Year < temp.Year)
            {
                dateLabel.Text = dateLabel.Text + " - " + temp.Year.ToString() + "년 " + temp.Month.ToString() + "월 " + temp.Day.ToString() + "일";
            }
            else if (temp.AddDays(-6).Month > temp.Month)
            {
                dateLabel.Text = dateLabel.Text + " - " + temp.Month.ToString() + "월 " + temp.Day.ToString() + "일";
            }
            else if (temp.AddDays(-6).Month < temp.Month)
            {
                dateLabel.Text = dateLabel.Text + " - " + temp.Month.ToString() + "월 " + temp.Day.ToString() + "일";
            }
            else
            {
                dateLabel.Text = dateLabel.Text + " - " + temp.Day.ToString() + "일";
            }
        }

        public void setDateLabel()
        {
            dateLabel.Text = monthCalendar1.SelectionStart.Year.ToString() + "년 " +
                             monthCalendar1.SelectionStart.Month.ToString() + "월 " +
                             monthCalendar1.SelectionStart.Day.ToString() + "일" + " (" +
                             monthCalendar1.SelectionStart.DayOfWeek.ToString() + ")";
        }

        private void reFlashButton_Click(object sender, EventArgs e)
        {
         
            CalendarThread ct;
            ct = new CalendarThread(this, plannerDay, plannerWeek, plannerMonth);
            
            ThreadStart ts = new ThreadStart(ct.initSycThread);
            Thread t = new Thread(ts);
            t.Start();
         
         /*   
            CalendarConnection.Instance().clearAllCalendar();
          */
        }

    }
}
